﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace PRE
{
    /// <summary>
    /// Author:		Nguyen Van Toan
    /// Email:		nvt87x@gmail.com
    /// Phone:		01645 515 010
    /// Create:		19/02/2012 08:00 AM
    /// Update:		?
    /// Main form: the form first show
    /// </summary>
    public partial class FrmMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        /// <summary>
        /// Current session login of user
        /// </summary>
        public static UTL.Session _sss = new UTL.Session();

        public FrmMain()
        {
            InitializeComponent();

            // Check license            
            if (_sss.License == UTL.Session.LicState.None)
                bbiRegistry_ItemClick(null, null);
            else bbiRegistry.Enabled = false;

            bsiUser.Caption = _sss.Name; // show name user current login
        }

        #region System
        private void bbiLogin_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            const string STR_LOGIN = "Đăng &nhập";
            const string STR_LOGOUT = "Đăng &xuất";

            if (bbiLogin.Caption == STR_LOGOUT)
            {
                _sss.Login = false;
                bbiLogin.Caption = STR_LOGIN;
                bbiLogin.LargeGlyph = Properties.Resources.login;

                bbiCloseAll_ItemClick(sender, e);
                ShowMenu(false); // hide menu
            }

            using (var frm = new Systym.FrmLogin())
            {
                frm.ShowDialog();
            }

            if (_sss.Login)
            {
                bbiLogin.Caption = STR_LOGOUT;
                bbiLogin.LargeGlyph = Properties.Resources.logout;

                ShowMenu(); // show menu
                ShowDefault();
            }
            else ShowMenu(false); // hide menu            
        }

        private void cbbSetting_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Systym.FrmSetting) return;
            var frm = new Systym.FrmSetting() { MdiParent = this };
            frm.Show();
        }

        private void bbiDefault_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Sumary.FrmDefault) return;
            var frm = new Sumary.FrmDefault() { MdiParent = this };
            frm.Show();
        }

        private void bbiRegistry_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (var frm = new Systym.FrmLicense())
            {
                frm.ShowDialog();
            }
        }

        private void bbiCloseAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            foreach (var frm in MdiChildren) frm.Close();
        }

        private void bbiExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Application.Exit();
        }

        private void bbiInfo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (var frm = new Systym.FrmInfo())
            {
                frm.ShowDialog();
            }
        }
        #endregion

        #region Catalog
        private void bbiUser_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Catalog.FrmUser) return;
            var frm = new Catalog.FrmUser() { MdiParent = this };
            frm.Show();
        }

        private void bbiGuest_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Catalog.FrmGuest) return;
            var frm = new Catalog.FrmGuest() { MdiParent = this };
            frm.Show();
        }

        private void bbiRoom_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Catalog.FrmRoom) return;
            var frm = new Catalog.FrmRoom() { MdiParent = this };
            frm.Show();
        }

        private void bbiRoomType_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Catalog.FrmRoomType) return;
            var frm = new Catalog.FrmRoomType() { MdiParent = this };
            frm.Show();
        }

        private void bbiLease_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Catalog.FrmLease) return;
            var frm = new Catalog.FrmLease() { MdiParent = this };
            frm.Show();
        }

        private void bbiStatus_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Catalog.FrmStatus) return;
            var frm = new Catalog.FrmStatus() { MdiParent = this };
            frm.Show();
        }

        private void bbiFee_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Catalog.FrmFee) return;
            var frm = new Catalog.FrmFee() { MdiParent = this };
            frm.Show();
        }

        private void bbiReceipt_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Catalog.FrmReceipt) return;
            var frm = new Catalog.FrmReceipt() { MdiParent = this };
            frm.Show();
        }
        #endregion

        private void FrmMain_Load(object sender, EventArgs e)
        {
            bsiServer.Caption = String.Format("[SERVER:{0} | DB:{1}]",
                                    DAL.BaseDAL.CurrentConnectInfo.Server,
                                    DAL.BaseDAL.CurrentConnectInfo.Database);
            var sql = new DAL.SQLServer();
            if (!sql.CheckDbExists(DAL.SQLServer.DbName))
                cbbSetting_ItemClick(sender, null); // show setting form
            else bbiLogin_ItemClick(sender, null); // show login form

#if DEBUG
            tmrMain.Enabled = false; // turn of timer
            ShowMenu(); // show menu not login

            _sss.Id = 1; // default user login
            ShowDefault();
#endif
            tmrMain_Tick(sender, e);
        }

        private void tmrMain_Tick(object sender, EventArgs e)
        {
            if (_sss.Current != null)
            {
                bsiTime.Caption = _sss.Current.Value.ToString("dd/MM/yyyy hh:mm:ss");
                _sss.Current = _sss.Current.Value.AddSeconds(1);
            }

            //bsiTime.Caption = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss");
        }

        /// <summary>
        /// Default fomr show all rooms
        /// </summary>
        private void ShowDefault()
        {
            try
            {
                var frm = new Sumary.FrmDefault() { MdiParent = this };
                frm.Show();
            }
            catch { return; }
        }

        /// <summary>
        /// Show menu
        /// </summary>
        /// <param name="isShow">Show is true else hide</param>
        private void ShowMenu(bool isShow = true)
        {
            rbpCatalog.Visible = isShow;
            rbpSumary.Visible = isShow;

            bsiUser.Caption = _sss.Name; // show name user current login
        }
    }
}